package simpleHttpClient;

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Driver class for the simple http client.
 * 
 * @author Rebecca Chandler
 * Scott Wendlandt
 * Li Weihan
 *
 */

public class SimpleHttpClient {

	public static void main (String args[]){
		ConsoleUI ui = new ConsoleUI(new Scanner (System.in), new PrintWriter(System.out, true));
		ui.displayResponse("Please select a location to store downloaded files.");
		File fileLocation = ui.promptFileLocation();

		//condition determines whether the while loop should continue running
		boolean keepRunning = true;
		
		//variables to be reused within the while loop
		Response response;
		ClientServerConnection connection;
		Request[] requests;

		//This while loop continues prompting the user a action and executing the selected action until the user selects exit
		while(keepRunning)
			switch(ui.promptNextAction()) {
			
			//executes an http request and displays the response
			case HTTP_REQUEST:
				connection = new ClientServerConnection(fileLocation);
				Request test = ui.promptRequest();
				ui.displayResponse(test.toString());
				response = connection.initialRequest(test);
				connection.close();
				
				//display response if an error did not occur
				if (response == null){
					ui.displayResponse("An error occured in processing the http request.  Request may be malformed.");
				} else{
					ui.displayResponse(""+response);
				}
				break;

			//prompts the user for a location to store the downloaded files and stores his choice
			case CHANGE_FILE_LOC:
				fileLocation = ui.promptFileLocation();
				ui.displayResponse("File Location Changed: " + fileLocation.getAbsolutePath());
				break;

			//prompts the user to select an html file in which to scan for downloadable elements.  downloads those elements and displays a response.
			case DL_HTML_ELEMENTS:
				File htmlFile = ui.promptHtmlFile();
				
				requests = null; //= someMethod(htmlFile); //calls a method to read url elements from file and makes them into a collection of requests
				
				//creates a directory named after the html file in which to store the downloaded files
				File htmlDir = new File (fileLocation, htmlFile.getName());
				htmlDir.mkdir();
				
				//creates a ClientServerConnection which will be used for persistent connections
				//(expects requests of the same host to be grouped together and to have the persistent connect indicated in their request)
				connection = new ClientServerConnection(htmlDir);
				String hostName = "";
				
				ui.displayResponse("/r/n---Generating html element responses---");
				//iterates through the requests array
				for(Request r: requests){
					//uses a persistent connection if the host name is the same as the previous
					if(r.getHostName().equals(hostName)){
						response = connection.followUpRequest(r);
					
					//creates a new connection if the host name is different than the previous
					} else {
					response = connection.initialRequest(r);
					
					
					hostName = r.getHostName();
					
					//display response if an error did not occur
					if (response == null){
						ui.displayResponse("An error occured in processing the http request: " + r.getHostName() + r.getObjectPathName() +
								"\nWithin html document: " + htmlFile.getName());
					} else{
						ui.displayResponse(""+response);
					}
				}
				ui.displayResponse("---Html elements downloaded.---/r/n" +
						"Files saved at " + htmlDir);
			case EXIT:
				keepRunning = false;
				break;

			default:
				break;

			}

		ui.close();
	}
}
